package rsschart.services.xml;

import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

import rsschart.charter.Charter;
import rsschart.charter.CharterFactory;
import rsschart.feed.Category;
import rsschart.feed.DataSet;
import rsschart.feed.DataSets;
import rsschart.feed.RssChart;
import rsschart.services.Processor;

public class XmlProcessor implements Processor {

	/**
	 * 	<chart>
	 * 		<cfg>
	 * 			<title>Pets that Jenny Wants</title>
	 * 			<type>bar</type>
	 * 			<w>600</w>
	 * 			<h>450</h>
	 * 			<link>http://www.iguanatronics.com</link>
	 * 		</cfg>
	 * 		<catg>
	 * 			<name>Pet Type</name>
	 * 			<val>Dog:Cat:Horse:Bird</val>
	 * 		</catg>
	 * 		<dss>
	 * 			<name>Pets</name>
	 * 			<valLbl>Count</valLbl>
	 * 			<ds>
	 * 				<name>Pet Count</name>
	 * 				<val>3:5:2:1</val>
	 * 			</ds>
	 * 		</dss>
	 * 	</chart>
	 */
	
	public String convertToSvg(String xml) throws Exception {

		String input;
		String chartFeed;
		Digester digester;
		ChartBean chartBean;
		ConfigBean cfgBean;
		RssChart chart;
		CategoryBean catgBean;
		Category catg;
		DataSetsBean dssBean;
		DataSets dss;
		DataSetBean dsBean;
		DataSet ds;
		String chartType;
		Writer writer;
		String content;
		Iterator dssIter;
		
		input = xml;
		try {
			digester = digest(input);
		}
		//-- decide if we will catch specifc exceptions
		catch (Exception e) {
			throw new ServletException(e);
		}
		
		
		chartBean = (ChartBean)digester.getRoot();
		chart = new RssChart();
		cfgBean = chartBean.getConfig();
		//-- to do: make sure width and height are consistent 4 to 3...
		chart.setWidth(Integer.parseInt(cfgBean.getWidth()));
		chart.setHeight(Integer.parseInt(cfgBean.getHeight()));
		chart.setTitle(cfgBean.getTitle());
		chart.setLink(cfgBean.getLink());
		
		catgBean = chartBean.getCategory();
		catg = new Category();
		catg.setName(catgBean.getName());
		catg.setValueString(catgBean.getValues());
		chart.setCategory(catg);
		
		dssBean = chartBean.getDataSets();
		dss = new DataSets();
		dss.setName(dssBean.getName());
		dss.setValueLabel(dssBean.getValueLabel());
		
		dssIter = dssBean.iterator();
		while (dssIter.hasNext()) {
			ds = new DataSet();
			dsBean = (DataSetBean)dssIter.next();
			ds.setName(dsBean.getName());
			ds.setValueString(dsBean.getValues());
			dss.add(ds);
		}
		chart.setDataSets(dss);
	
		Charter charter;
		chartType = cfgBean.getType();
		try {
			charter = CharterFactory.getCharter(chartType);	
		}
		catch (Exception e) {
			// to do... decide what to do
			throw new Exception(e);
		}
		content = charter.draw(chart);
		
		return content;
	}

	public Digester digest(String input) throws Exception {
	    String output;
	    StringReader reader;
	    
	    reader = new StringReader(input);
	    
		Digester digester = new Digester();
	    digester.setValidating( false );
	
	    digester.addObjectCreate( "chart", ChartBean.class );
	    digester.addObjectCreate( "chart/cfg", ConfigBean.class );
	    digester.addBeanPropertySetter( "chart/cfg/title", "title" );
	    digester.addBeanPropertySetter( "chart/cfg/type", "type" );
	    digester.addBeanPropertySetter( "chart/cfg/w", "width" );
	    digester.addBeanPropertySetter( "chart/cfg/h", "height" );
	    digester.addBeanPropertySetter( "chart/cfg/link", "link" );
	    digester.addSetNext("chart/cfg","addConfig");
	    
	    digester.addObjectCreate( "chart/catg", CategoryBean.class );
	    digester.addBeanPropertySetter( "chart/catg/name", "name" );
	    digester.addBeanPropertySetter( "chart/catg/val", "values" );
	    digester.addSetNext("chart/catg","addCategory");	    
	    
	    digester.addObjectCreate( "chart/dss", DataSetsBean.class );
	    digester.addBeanPropertySetter( "chart/dss/name", "name" );
	    digester.addBeanPropertySetter( "chart/dss/valLbl", "valueLabel" );
	    digester.addSetNext("chart/dss","addDataSets");
	
	    digester.addObjectCreate( "chart/dss/ds", DataSetBean.class );		    
	    digester.addBeanPropertySetter( "chart/dss/ds/name", "name" );
	    digester.addBeanPropertySetter( "chart/dss/ds/val", "values" );
	    digester.addSetNext("chart/dss/ds","addDataSet");
	    
	    try {
	    	digester.parse(reader);
	    }
	    catch (SAXException se) {
	    	throw new Exception("to do: " + se.getMessage());
	    }
	    catch (Exception e) {
	    	throw new Exception("to do: " + e.getMessage());
	    }
	
		return digester;
	
	}




	
}
